package com.jf.mall.web;

import com.jf.mall.domain.User;
import com.jf.mall.service.UserService;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-09-26
 * Time:15:20
 */
@WebServlet(name = "AdminServlet", urlPatterns = "/admin/adminServlet")
public class AdminServlet extends BasicServlet{
    private UserService userService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        userService = new UserService();
    }

    private void loginAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String username = req.getParameter("user-name");
        String password = req.getParameter("user-password");
        User tempUser = new User(username, password);
        User admin = userService.loginAdmin(tempUser);
        String contextPath = req.getServletContext().getContextPath();
        if (admin != null) {
            HttpSession session = req.getSession();
            session.setAttribute("admin", admin);
            resp.sendRedirect(contextPath + "/views/admin/admin_menu.jsp");
        } else {
            resp.sendRedirect(contextPath + "/index.jsp");
        }
    }
}
